package com.research.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.research.common.CustomException;
import com.research.common.R;
import com.research.entity.FundingApproval;
import com.research.entity.ResearchProject;
import lombok.extern.slf4j.Slf4j;
import com.research.service.FundingApprovalService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.research.mapper.ResearchProjectMapper;
/*前端控制器*/
@Slf4j
@RestController
@RequestMapping("/fundingApproval")
public class FundingApprovalController extends ServiceImpl<ResearchProjectMapper, ResearchProject> {

    @Resource
    private FundingApprovalService fundingApprovalService;

    @GetMapping("list")
    public R list(String keyword) {
        return R.success(fundingApprovalService.list(
                new QueryWrapper<FundingApproval>().eq(keyword != null, "project_id", keyword)
        ));
    }

    @PostMapping("save")
    public R save(@RequestBody FundingApproval fundingApproval) {
        return R.success(fundingApprovalService.save(fundingApproval));
    }

    @PostMapping("update")
    public R update(@RequestBody FundingApproval fundingApproval) {
        QueryWrapper<ResearchProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id", fundingApproval.getProjectId());
        if(baseMapper.selectOne(queryWrapper)!=null){
            return R.success(fundingApprovalService.updateById(fundingApproval));
            }
        else{
            throw new CustomException("请填写正确项目id");
        }
    }

    @PostMapping("delete")
    public R delete(Integer id) {
        return R.success(fundingApprovalService.removeById(id));
    }

    @GetMapping("detail")
    public R detail(Integer id) {
        return R.success(fundingApprovalService.getById(id));
    }


}

